/** \file tim_contol.c Contains the definition for functions and variables of the tim_control component.
*/

#include "tim_control.h"
#include "LOC_scrnframe.h"
#include "srg_dirver.h"


/*********************/
/*	Local Dfinitions */
/*********************/

#define TIM_u8TIMER0_INIT_CTC               (u8)0x0A

#define TIM_u8TIMER1A_INIT_CTC              (u8)0x00

#define TIM_u8TIMER1B_INIT_CTC              (u8)0x0A

#define TIM_u16TIMER1_INIT_50MS             (u16)49999

#define TIM_u8TIMER0_INIT_200US             (u8)199
 
#define TIM_u8SCREEN_MAX                    (u8)15 /*MAIN_nSCREEN*/

/**********************/
/*	external Variables*/
/**********************/

volatile TYP_tu8Bool TIM_bBackgroundFlag;

/*******************/
/*	Local Variables*/
/*******************/

static u8 u8ScreenCounter;

/*********************************/
/*	Local Functions' Declaration */
/*********************************/




/***********************************/
/*	External Functions' Definition */
/***********************************/

/* The used crystal is 8 MHz*/
void TIM_vidInit(void)
{
    cli();

    TIM_bBackgroundFlag = TYP_u8FALSE;
    u8ScreenCounter = TIM_u8SCREEN_MAX;
	/* Timer 0 initialization. Screen Display timer. */
	OCR0 = TIM_u8TIMER0_INIT_200US;
    TIMSK |= (1<<OCIE0);
    TCCR0 = TIM_u8TIMER0_INIT_CTC;
 
    


	/* Timer 1 initialization. Cyclic timer.*/
    OCR1A = TIM_u16TIMER1_INIT_50MS;
    TCCR1A = TIM_u8TIMER1A_INIT_CTC;    
    TIMSK |= (1<<OCIE1A);
    TCCR1B = TIM_u8TIMER1B_INIT_CTC;
    sei();
	/* Watchdog timer initialization.*/


}


//Background Timer
ISR(TIMER1_COMPA_vect)
{

    TIM_bBackgroundFlag = TYP_u8TRUE;

}

//Screen Display timer.
ISR(TIMER0_COMP_vect)
{
    u8 u8TempData;

    u8TempData = SCR_u8GetReadyByte();
    if (u8ScreenCounter == TIM_u8SCREEN_MAX)
    {
        u8ScreenCounter = TYP_u8ZERO;
        SRG_vidPutData(u8TempData, TYP_u8TRUE);
    }
    else
    {
        
        SRG_vidPutData(u8TempData, TYP_u8FALSE);
    }
    u8ScreenCounter++;

}
/********************************/
/*	Local Functions' Definition */
/********************************/
